package com.gg.widget

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.graphics.drawable.Drawable
import android.graphics.Canvas
import android.graphics.Paint


/**
 * Creator : GG
 * Date    : 2018/4/8
 * Mail    : gg.jin.yu@gmai.com
 * Explain :
 */
class GridItemDecoration : RecyclerView.ItemDecoration {


    private var mDividerDarwable: Drawable? = null
    private var mDividerHight = 1
    private val mColorPaint: Paint? by lazy { Paint() }


    val ATRRS = intArrayOf(android.R.attr.listDivider)

    constructor(context: Context) {
        val ta = context.obtainStyledAttributes(ATRRS)
        this.mDividerDarwable = ta.getDrawable(0)
        ta.recycle()
    }

    /**
     *int dividerHight 分割线的线宽
     *int dividerColor 分割线的颜色
     */
    constructor(context: Context, dividerHight: Int, dividerColor: Int) : this(context) {
        mDividerHight = dividerHight
        mColorPaint!!.color = dividerColor
    }

    /**
     *int dividerHight 分割线的线宽
     *Drawable dividerDrawable 图片分割线
     */
    constructor(context: Context, dividerHight: Int, dividerDrawable: Drawable) : this(context) {

        mDividerHight = dividerHight
        mDividerDarwable = dividerDrawable
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        //画水平和垂直分割线
        drawHorizontalDivider(c, parent)
        drawVerticalDivider(c, parent)
    }

    private fun drawVerticalDivider(c: Canvas, parent: RecyclerView) {
        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams

            val top = child.top - params.topMargin
            val bottom = child.bottom + params.bottomMargin

            var left = 0
            var right = 0

            //左边第一列
            if (i % 3 == 0) {
                //item左边分割线
                left = child.left
                right = left + mDividerHight
                mDividerDarwable!!.setBounds(left, top, right, bottom)
                mDividerDarwable!!.draw(c)
                if (mColorPaint != null) {
                    c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mColorPaint)
                }
                //item右边分割线
                left = child.right + params.rightMargin - mDividerHight
                right = left + mDividerHight
            } else {
                //非左边第一列
                left = child.right + params.rightMargin - mDividerHight
                right = left + mDividerHight
            }
            //画分割线
            mDividerDarwable!!.setBounds(left, top, right, bottom)
            mDividerDarwable!!.draw(c)
            if (mColorPaint != null) {
                c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mColorPaint)
            }

        }
    }

    private fun drawHorizontalDivider(c: Canvas, parent: RecyclerView) {

        val childCount = parent.childCount
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val params = child.layoutParams as RecyclerView.LayoutParams

            val left = child.left - params.leftMargin - mDividerHight
            val right = child.right + params.rightMargin
            var top = 0
            var bottom = 0

            // 最上面一行
            if (i / 3 == 0) {
                //当前item最上面的分割线
                top = child.top
                //当前item下面的分割线
                bottom = top + mDividerHight
                mDividerDarwable!!.setBounds(left, top, right, bottom)
                mDividerDarwable!!.draw(c)
                if (mColorPaint != null) {
                    c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mColorPaint)
                }
                top = child.bottom + params.bottomMargin
                bottom = top + mDividerHight
            } else {
                top = child.bottom + params.bottomMargin
                bottom = top + mDividerHight
            }
            //画分割线
            mDividerDarwable!!.setBounds(left, top, right, bottom)
            mDividerDarwable!!.draw(c)
            if (mColorPaint != null) {
                c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(), mColorPaint)
            }
        }
    }

}